<?php
namespace app\models;
use ac\component\tool\Tool;
use app\models\Basic;

use Yii;
use yii\helpers\Html;

class Address extends Basic
{
    
    public static function tableName()
    {
        return '{{%pj8_member_address}}';
    }

    public function rules()
    {
        return [
            ['member_id', 'required', 'message' => 'member_id不能为空', 'on' => 'add'],
            ['member_id', 'integer', 'message' => 'member_id必须为数字，或数字字符串', 
                         'min'=>1, 'tooSmall' => 'member_id最小存储1'],
            ['consignee', 'string', 'message' => 'consignee必须是字符串', 
                         'max' => 64, 'tooLong' => 'consignee最长64位'],
            ['sex', 'integer', 'message' => 'sex必须为数字，或数字字符串'],
            ['sex', 'in', 'range' => array(1, 2), 'message' => 'sex值必须在1~2中选择'],
            ['phone', 'required', 'message' => 'phone不能为空', 'on' => 'add'],
            ['phone', 'string', 'message' => 'phone必须是字符串',
                                'min' => 1, 'tooShort' => 'phone最短1位',
                                'max' => 18, 'tooLong' => 'phone最长18位'],
            // ['province', 'default', 'value' => ''],
            ['province', 'string', 'message' => 'province必须是字符串', 
                         'max' => 16, 'tooLong' => 'province最长16位'],
            // ['city', 'default', 'value' => ''],
            ['city', 'string', 'message' => 'city必须是字符串', 
                     'max' => 16, 'tooLong' => 'city最长16位'],
            // ['district', 'default', 'value' => ''],
            ['district', 'string', 'message' => 'district必须是字符串', 
                         'max' => 16, 'tooLong' => 'district最长16位'],
            ['address', 'default', 'value' => ''],
            ['address', 'string', 'message' => 'address必须是字符串', 
                         'max' => 128, 'tooLong' => 'address最长128位'],
            ['zipcode', 'string', 'message' => 'zipcode必须是字符串', 
                         'max' => 16, 'tooLong' => 'zipcode最长16位'],
            ['sign_building', 'string', 'message' => 'sign_building必须是字符串', 
                         'max' => 64, 'tooLong' => 'sign_building最长64位'],
            ['is_default', 'integer', 'message' => 'is_default必须为数字，或数字字符串'],
            ['is_default', 'in', 'range' => array(1, 2), 'message' => 'is_default值必须在1~2中选择'],
        ];
    }


    /*添加*/
    public function addAddress($data, $mid)
    {
        // P($data);
        $this->scenario = 'add';
        // P($data);
        if($this->load($data) and $this->validate()){
            // P($this);
            $transaction = Yii::$app->db->beginTransaction();//事物处理
            try{
                if(isset($data['Address']['is_default']) and $data['Address']['is_default'] == 2){
                    /*把其他的地址都設置成普通地址，先添加的是默認地址*/
                    $this->updateAll(['is_default'=>1], ['member_id' => $mid]);
                }
                $this->save(false);
                
                $transaction->commit();            
                return true;
            }catch(\Exception $e){
                $transaction->rollback();
                throw new \Exception();         
                return false;
            };
        }
        return false;
    }

    /*
    修改
    $data       修改的数据
    */
    public function modifyAddress($data, $id, $mid)
    {
        // P($id);
        $address = self::find()->where('member_id = :uid and address_id = :id', [':uid' => $mid, ':id' => $id])->one();
        // P($address);
        // P($data['Address']['sex']);
        if(!is_null($address)){
            if($this->load($data) and $this->validate()){
                // P($data);
                $transaction = Yii::$app->db->beginTransaction();//事物处理
                try{
                    /*如果需要设置成默认地址，那需要把其他的地址修改成普通地址>>>*/
                    if(isset($data['Address']['is_default']) and $data['Address']['is_default'] == 2){
                        $this->updateAll(['is_default'=>1], ['member_id'=>$mid]);
                    }
                    /*如果需要设置成默认地址，那需要把其他的地址修改成普通地址<<<*/
                    if(isset($data['Address']['consignee'])) {
                        $address->consignee = $data['Address']['consignee'];
                    }
                    if(isset($data['Address']['sex'])) {
                        $address->sex = $data['Address']['sex'];
                    }
                    if(isset($data['Address']['phone'])) {
                        $address->phone = $data['Address']['phone'];
                    }
                    if(isset($data['Address']['province'])) {
                        $address->province = $data['Address']['province'];
                    }
                    if(isset($data['Address']['city'])) {
                        $address->city = $data['Address']['city'];
                    }
                    if(isset($data['Address']['district'])) {
                        $address->district = $data['Address']['district'];
                    }
                    if(isset($data['Address']['address'])) {
                        $address->address = $data['Address']['address'];
                    }
                    if(isset($data['Address']['zipcode'])) {
                        $address->zipcode = $data['Address']['zipcode'];
                    }
                    if(isset($data['Address']['sign_building'])) {
                        $address->sign_building = $data['Address']['sign_building'];
                    }
                    if($address->is_default != 2){//不能把唯一的默认地址取消掉，必须要有一个默认地址
                        $address->is_default = $data['Address']['is_default'];
                    }
                    // P($address);
                    $address->save(false);
                    $transaction->commit();            
                    return true;
                }catch(\Exception $e){
                    // $transaction->rollback();
                    // throw new \Exception();   
                    // P($e);      
                    return false;
                };
                return false;
            }
        };
        return false;
    }


    public function getMember()
    {
        $info = $this->hasOne(Member::className(), ['member_id' => 'member_id'])
                    ->select(['{{%pj8_member}}.member_id', 'nickname', 'headimgurl']);
        return $info;
    }

    
}